/*
 * Function_new_Pool.h
 *
 *  Created on: 2014. 6. 23.
 *      Author: jjaehuny
 */

#ifndef FRAMEWORK_MEMORY_FUNCTION_NEW_POOL_H_
#define FRAMEWORK_MEMORY_FUNCTION_NEW_POOL_H_

#include <glog/logging.h>

#include "Base_Pool.h"

namespace Framework { namespace Memory {

template<class Type>
class Function_new_Pool : public Base_Pool<Type>
{
public:
	Function_new_Pool (bool bPost = false, unsigned long pool_size = 1);
	virtual ~Function_new_Pool ();

protected:
	virtual Type * allocator ();
	virtual void deleter (Type * pType);

};

template<class Type>
Function_new_Pool<Type>::Function_new_Pool (bool bPost, unsigned long pool_size) : Base_Pool<Type>(bPost, pool_size)
{

}

template<class Type>
Function_new_Pool<Type>::~Function_new_Pool ()
{

}

template<class Type> Type *
Function_new_Pool<Type>::allocator ()
{
	LOG(INFO) << __PRETTY_FUNCTION__;

	Type * pType = NULL;
	{
		if (true != Base_Pool<Type>::type_deque_.empty())
		{
			pType = Base_Pool<Type>::type_deque_.front();
			Base_Pool<Type>::type_deque_.pop_front();
		}
		else
		{
			pType = new Type;
		}
	}

	// placement new
	if (NULL != pType)
	{
		pType->Initialize();
	}

	return pType;
}

template<class Type> void
Function_new_Pool<Type>::deleter (Type * pType)
{
	if (NULL != pType)
	{
		// placement delete
		pType->Finalize();

		LOG(INFO) << __PRETTY_FUNCTION__;

		Base_Pool<Type>::type_deque_.push_back(pType);
	}
}

} /*Memory*/ } /*Framework*/

#endif /* FRAMEWORK_MEMORY_FUNCTION_NEW_POOL_H_ */
